package com.evaluation.kexingcp.psycho.evaluation.sys.evaluation

import com.alibaba.fastjson.JSON
import com.evaluation.kexingcp.common.core.util.R
import com.evaluation.kexingcp.common.security.annotation.Inner
import com.evaluation.kexingcp.psycho.evaluation.sys.entity.EvaluationGeneralResultEntity
import jakarta.servlet.ServletRequest
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.RestController
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.PostMapping

data class AnswerOption(val id: Int, val score: Int, val checked: Boolean, val selected: String)
data class Answer(val id: Int, val subject: String, val isMultiple: Boolean, val option: List<AnswerOption>) {
    fun getScoreAndType() = if (isMultiple) {
        val options = booleanArrayOf(false, false, false, false, false, false, false)

        for (answerOption in option) {
            if (answerOption.checked)
                options[answerOption.id] = true
        }

        Pair("P", options)
    } else {
        Pair("Q", option.first { it.checked }.score)
    }

    fun getSelectIdx(): Int {
        var res = 1
        for ((idx, answerOption) in option.withIndex()) {
            if (answerOption.checked)
                res = idx + 1
        }

        return res
    }
}

data class AnswersRequest(val answers: List<Answer> = mutableListOf())

class GeneralRequest {
    var user: EvaluationGeneralResultEntity? = null
    var answer: List<Answer> = mutableListOf()
}

@RestController
@RequestMapping("/evaluation")
open class EvaluationController(
    private val evaluationService: EvaluationService
) {

    @PostMapping("/{taskId}/{scaleId}")
    open fun evaluationScale(@PathVariable taskId: Long, @PathVariable scaleId: Long, request: ServletRequest)
    : R<Unit> {

        val answers = JSON.parseObject<AnswersRequest>(request.inputStream.readBytes(), AnswersRequest::class.java)

        return evaluationService.evaluationScale(taskId, scaleId, answers).fold(
            ifLeft = { err ->
                R.failed(err.message)
            },
            ifRight = {
                R.ok(null, "操作成功")
            }
        )
    }

    @PostMapping("/{taskId}/{scaleId}/{userId}")
    open fun evaluationScale(@PathVariable taskId: Long, @PathVariable scaleId: Long, @PathVariable userId: Long, request: ServletRequest)
            : R<Unit> {

        val answers = JSON.parseObject<AnswersRequest>(request.inputStream.readBytes(), AnswersRequest::class.java)

        return evaluationService.evaluationScale(taskId, scaleId, userId, answers).fold(
            ifLeft = { err ->
                R.failed(err.message)
            },
            ifRight = {
                R.ok(null, "操作成功")
            }
        )
    }


    @Inner(value = false)
    @PostMapping("/general/{tenantId}/{taskId}/{scaleId}")
    open fun generalEvaluation(@PathVariable tenantId: Long, @PathVariable taskId: Long, @PathVariable scaleId: Long, request: ServletRequest)
            : R<Unit> {

        val answers = JSON.parseObject<GeneralRequest>(request.inputStream.readBytes(), GeneralRequest::class.java)

        return evaluationService.generalEvaluation(tenantId, taskId, scaleId, answers).fold(
            ifLeft = { err ->
                R.failed(err.message)
            },
            ifRight = {
                R.ok(null, "操作成功")
            }
        )
    }
}